package main

import (
	"fmt"
	"html/template"
	"io"
	"net/http"
)

func main() {
	http.HandleFunc("/", Hey)
	http.ListenAndServe(":80", nil)
}

// ``符号支持多行字符串
const tpl = `
<html>
<head>
    <title>Hey</title>
</head>
<body>
    <form method="post" action="/">
        Username:<input name="uname">
        Password:<input type="password" name="pwd">
        <button type="submit">Submit</button>
    </form>
</body>
</html>
`

func Hey(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		t := template.New("hey")
		t.Parse(tpl)
		t.Execute(w, nil)
	} else {
		// r.FormValue(key)是go获取表单数据的方式
		io.WriteString(w, fmt.Sprintf("姓名：%s\n密码：%s", r.FormValue("uname"), r.FormValue("pwd")))
	}
}
